<?php
/*
Plugin Name: 设备管理
Plugin URI: http://wpjam.net/item/wpjam-basic/
Description: 手机设备管理，包括增加，修改，删除。
Version: 1.0
*/

global $wpdb;
$wpdb->devices		= $wpdb->base_prefix . 'devices';

wp_cache_add_global_groups(array('wpjam-device'));

// 获取所有移动设备
function wpjam_get_devices($return = ''){
	$devices	= wp_cache_get('all','wpjam_device');
	if($devices === false){
		global $wpdb;
		$devices	= $wpdb->get_results("SELECT * FROM {$wpdb->devices}", ARRAY_A);
		wp_cache_set('all', $devices, 'wpjam-device', 30);
	}

	if($return == 'all'){
		return $devices;
	}else{
		$new_devices = array();
		foreach ($devices as $device) {
			$device_key = strtoupper($device['device']);
			$new_devices[$device_key] = $device['name'];
		}
		return $new_devices;
	}
}



function wpjam_get_apple_devices(){
	$devices	= wp_cache_get('apple','wpjam-device');
	if($devices === false){
		global $wpdb;
		$devices	= $wpdb->get_results("SELECT device,name FROM {$wpdb->devices} WHERE brand='Apple'", OBJECT_K);
		wp_cache_set('apple', $devices, 'wpjam-device', 3600);
	}

	return $devices;
}

// 获取具体某款设备的信息
function wpjam_get_device($device = ''){
	$device_array = wp_cache_get($device, 'wpjam-device');

	if($device_array === false){
		global $wpdb;
		$device_array = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->devices} WHERE device = %s",$device), ARRAY_A);
		wp_cache_set($device, $device_array, 'wpjam-device', 36000);
	}
	
	return $device_array;
}
